<?php
if(!defined('BASE_DIR'))die('Cannot access the file !');
/**
 * -+-----------------------------------
 * |PHP Framework
 * |Copyright (c) 2010 jinke.[iblue.cc]
 * |E-mail: mejinke@gmail.com
 * |Date: 2011-09-16
 * -+-----------------------------------
 *
 * 用户状态管理类
 * @author jinke
 */

class User extends Base
{
	/**
	 * 认证标识
	 * @var bool
	 */
	private $authenticated = false;
	
	/**
	 * COOKIE加密标识
	 * @var string
	 */
	private $cookieKey = null;
	
	/**
	 * 用户证书列表
	 * @var mixed
	 */
	private $credentials = null;
	
	
	public function __construct()
	{
		$this->cookieKey = $_SERVER['HTTP_HOST'].'_+i!~0';	
	}
	
	
	/**
	 * 设置是否认证通过
	 * @access public
	 * @param bool $status 状态    
	 * @return void
	 */
	public function setAuthenticated($status)
	{
		if (is_bool($status))
		{
			if ($status===false)
				$this->removeAllCredential();
			$this->authenticated = $status;
			$this->saveCookie();
		}
	}
	
	/**
	 * 获取认证状态标识
	 * @access public
	 * @return bool
	 */
	public function getAuthenticated()
	{
		return $this->authenticated;
	}
	
	
	/**
	 * 添加证书
	 * @access public
	 * @param mixed $var 证书
	 * @return void
	 */
	public function addCredential($var)
	{
		if ($this->authenticated === true)
		{
			$this->credentials[] = $var;
			$this->saveCookie();
		}
	}
	
	/**
	 * 批量添加证书
	 * @access public
	 * @param array $var 证书列表
	 * @return void
	 */
	public function addCredentials(Array $var)
	{
		for ($i=0; $i<count($var); $i++)
		{
			$this->addCredential($var[$i]);
		}
	}
	
	
	/**
	 * 删除证书
	 * @access public
	 * @param mixed $var 证书
	 * @return bool
	 */
	public function removeCredential($var)
	{
		if ($this->hasCredential($var))
		{
			$key = array_search($var, $this->credentials, true);
			unset($this->credentials[$key]);
		}
		if (empty($this->credentials))
			$this->setAuthenticated(false);
		$this->saveCookie();
		return true;	
	}
	
	/**
	 * 批量删除证书
	 * @access public
	 * @param array $var 证书列表
	 * @return bool
	 */
	public function removeCredentials(Array $var)
	{
		for ($i=0; $i<count($var); $i++)
		{
			$this->removeCredential($var[$i]);
		}
		return true;
	}
	
	
	/**
	 * 删除所有证书
	 * @access public
	 * @return void
	 */
	public function removeAllCredential()
	{
		$this->credentials = null;
		$this->authenticated = false;
		$this->saveCookie();
	}
	
	
	/**
	 * 检查用户是否拥有指定的证书
	 * @access public
	 * @param mixed $var 证书
	 * @return bool
	 */
	public function hasCredential($var)
	{
		if ($this->credentials === null) return false;
		if(array_search($var, $this->credentials, true)!==false)
			return true;
		return false;
	}
	
	/**
	 * 读取COOKIE资料
	 * @access public
	 * @return void
	 */
	public function readCookie()
	{	
		if (isset($_COOKIE[md5('FW_Credential'.$this->cookieKey)]))
		{ 
			$credentialValue = strDecrypt($_COOKIE[md5('FW_Credential'.$this->cookieKey)]);
			$authenticatedValue = strDecrypt($_COOKIE[md5('FW_Authenticated'.$this->cookieKey)]);
			$credentialValue = unserialize(str_replace('FW_Credential', '', $credentialValue));
			$authenticatedValue = unserialize(str_replace('FW_Authenticated', '', $authenticatedValue));
			$this->credentials = $credentialValue;
			$this->authenticated = $authenticatedValue;
		}
	}
	
	/**
	 * 保存COOKIE资料
	 * @access public
	 * @return void
	 */
	public function saveCookie()
	{
		$value = strEncrypt(serialize($this->credentials).'FW_Credential');
		$value1 = strEncrypt(serialize($this->authenticated).'FW_Authenticated');
		setcookie(md5('FW_Credential'.$this->cookieKey), $value, time()+60*15);
		setcookie(md5('FW_Authenticated'.$this->cookieKey), $value1, time()+60*15);
	}
	
	
	
	
	
}
?>